package com.cmas.platform.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cmas.platform.domain.Region;
import com.cmas.platform.domain.Section;
import com.cmas.platform.mapper.RegionMapper;
import com.cmas.platform.vo.RegionMapVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmas.platform.mapper.RegionMapMapper;
import com.cmas.platform.domain.RegionMap;
import com.cmas.platform.service.RegionMapService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 地区地图Service业务层处理
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-14
 */
@Service
public class RegionMapServiceImpl extends ServiceImpl<RegionMapMapper, RegionMap> implements RegionMapService {

    private static final Logger log = LoggerFactory.getLogger(RegionMapServiceImpl.class);

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询地区地图列表
     *
     * @param regionMap 地区地图
     * @return 地区地图
     */
    @Override
    public List<RegionMapVo> selectRegionMapVoList(RegionMap regionMap) {
        QueryWrapper<RegionMap> queryWrapper = setQueryWrapper(regionMap);
        return baseMapper.selectRegionMapList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRegionMap(RegionMap regionMap) {
        RegionMap oldRegionMap = baseMapper.selectById(regionMap.getId());
        if (!oldRegionMap.getRegionId().equals(regionMap.getRegionId())) {
            regionMapper.update(null, new UpdateWrapper<Region>().lambda()
                    .eq(Region::getId, oldRegionMap.getRegionId())
                    .set(Region::getStatus, "0"));
            regionMapper.update(null, new UpdateWrapper<Region>().lambda()
                    .eq(Region::getId, regionMap.getRegionId())
                    .set(Region::getStatus, "1"));
        }
        return baseMapper.updateById(regionMap);
    }


    @Override
    public List<RegionMap> selectRegionMapList(RegionMap regionMap) {
        QueryWrapper<RegionMap> queryWrapper = setQueryWrapper(regionMap);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<RegionMapVo> getOnlineRegionMap(RegionMapVo regionMapVo) {
        RegionMap regionMap = new RegionMap();
        BeanUtils.copyProperties(regionMapVo, regionMap);
        QueryWrapper<RegionMap> queryWrapper = setQueryWrapper(regionMap);
        if (StringUtils.isNotEmpty(regionMapVo.getStatus())) {
            queryWrapper.eq("region.status", regionMapVo.getStatus());
        }
        return baseMapper.selectRegionMapList(queryWrapper);
    }


    /**
     * 设置QueryWrapper条件
     *
     * @param
     * @return
     */
    public QueryWrapper<RegionMap> setQueryWrapper(RegionMap regionMap) {
        QueryWrapper<RegionMap> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(regionMap.getName())) {
            queryWrapper.like("region_map.`name`", regionMap.getName());
        }
        if (StringUtils.isNotEmpty(regionMap.getRegionId())) {
            queryWrapper.lambda().like(RegionMap::getRegionId, regionMap.getRegionId());
        }
        return queryWrapper;
    }

    /**
     * 批量删除地区地图
     *
     * @param ids 需要删除的地区地图ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteRegionMapByIds(String[] ids) {
        List<RegionMap> regionMapList = baseMapper.selectBatchIds(Arrays.asList(ids));
        String[] regionIds = regionMapList.stream().map(RegionMap::getRegionId).toArray(String[]::new);
        regionMapper.update(null, new UpdateWrapper<Region>().lambda()
                .in(Region::getId, regionIds).set(Region::getStatus, "0"));
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addRegionMap(RegionMap regionMap) {
        regionMapper.update(null, new UpdateWrapper<Region>().lambda()
                .eq(Region::getId, regionMap.getRegionId())
                .set(Region::getStatus, "1"));
        baseMapper.insert(regionMap);
        return true;
    }

}
